package cn.crdeng.controller.admin;

import cn.crdeng.Service.BlogService;
import cn.crdeng.Service.TagService;
import cn.crdeng.Service.TypeService;
import cn.crdeng.po.Blog;
import cn.crdeng.po.BlogQuery;
import cn.crdeng.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@Controller
@RequestMapping("/admin")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    @GetMapping("/blogs")
    public String blogPage(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                           BlogQuery blogQuery, Model model) {
        model.addAttribute("pageInfo", blogService.listBlog(pageable, blogQuery));
        model.addAttribute("types", typeService.findAll());
        return "admin/blog";
    }

    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                         BlogQuery blogQuery, Model model) {
        model.addAttribute("pageInfo", blogService.listBlog(pageable, blogQuery));
        blogService.listBlog(pageable, blogQuery);

        return "admin/blog :: blogList";
    }

    @GetMapping("/blogs/input")
    public String blogInputPage(Model model) {
        model.addAttribute("tags", tagService.listTag());
        model.addAttribute("types", typeService.findAll());
        model.addAttribute("blog", new Blog());
        return "admin/blog-input";
    }

    @GetMapping("/blogs/{id}/input")
    public String updateBlog(@PathVariable Long id, Model model) {
        model.addAttribute("tags", tagService.listTag());
        model.addAttribute("types", typeService.findAll());
        Blog blog = blogService.getBlogByID(id);
        blog.init();
        model.addAttribute("blog", blog);
        return "admin/blog-input";
    }

    @PostMapping("/blogs")
    public String saveBlog(@Valid Blog blog, BindingResult result, RedirectAttributes attributes, HttpSession session) {
        if (result.hasErrors()) {
            return "admin/blog-input";
        }
        if ("".equals(blog.getFlag())){
            blog.setFlag("原创");
        }
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));
        Blog saveBlog;
        if (blog.getId() == null) {
            saveBlog = blogService.save(blog);
        } else {
            saveBlog = blogService.updateBlog(blog.getId(), blog);
        }
        /*Blog saveBlog = blogService.save(blog);*/
        if (saveBlog == null) {
            //提示添加错误
            attributes.addFlashAttribute("message", "修改博客失败！");
        } else {
            //提示成功
            attributes.addFlashAttribute("message", "修改博客成功");
        }
        return "redirect:/admin/blogs";
    }

    @GetMapping("/blogs/{id}/delete")
    public String deleteBlog(@PathVariable Long id, RedirectAttributes attributes) {
        Blog blogByID = blogService.getBlogByID(id);
        if (blogByID != null) {
            blogService.delete(id);
            attributes.addAttribute("message", "删除成功");
        }
        return "redirect:/admin/blogs";
    }
}
